<?php
    header("Content-Type:text/html;charset=utf-8");
    //连接数据库
    require ('config.php');

    $data = json_decode(file_get_contents("php://input"), true);
    $fun = $data['fun'];
    if($fun == 'logonstate'){
        if($username)
     	{
     	    echo json_encode(array('stat'=>'200','msg'=>'你已经登录过了','username' => $username),JSON_UNESCAPED_UNICODE);
			exit();
     	}
    }elseif($fun == 'login'){
        $username = $data['username'];
        $qqnum = $data['qqnum'];
        $email = $data['email'];
        $ver = $data['ver'];
        $srcver = $data['srcver'];
        $password = $data['password'];
        $conpassword = $data['conpassword'];
        
        if(!$username or !$qqnum or !$email or !$ver or !$password or !$conpassword){
            echo json_encode(array('stat'=>'400','msg'=>'* 所有输入框都是必填项，请填写完整！'),JSON_UNESCAPED_UNICODE);
        }elseif($password != $conpassword){
            echo json_encode(array('stat'=>'400','msg'=>'* 密码和确认密码输入不一致！'),JSON_UNESCAPED_UNICODE);
        }elseif(strlen($username) < 2){
            echo json_encode(array('stat'=>'400','msg'=>'* 输入的用户名不合法（2~10位）'),JSON_UNESCAPED_UNICODE);
            
        }elseif(strlen($qqnum) < 6 or strlen($qqnum) > 10){
            echo json_encode(array('stat'=>'400','msg'=>'* 输入的QQ不合法（登陆凭证）'),JSON_UNESCAPED_UNICODE);
            
        }elseif(strlen($password) < 6){
            echo json_encode(array('stat'=>'400','msg'=>'* 设置的密码不符合规定（6~15位）'),JSON_UNESCAPED_UNICODE);
            
        }elseif(strlen($ver) != 5){
            echo json_encode(array('stat'=>'400','msg'=>'* 验证码错误！'),JSON_UNESCAPED_UNICODE);
            
        }else{
            $verif = 1;
            if($srcver==md5('0')){
                if(strtolower($ver)!='xzpvs')   $verif = 0;
            }elseif($srcver==md5('1')){
                if(strtolower($ver)!='bvs7u')   $verif = 0;
            }elseif($srcver==md5('2')){
                if(strtolower($ver)!='dam34')   $verif = 0;
            }elseif($srcver==md5('3')){
                if(strtolower($ver)!='dzfuv')   $verif = 0;
            }elseif($srcver==md5('4')){
                if(strtolower($ver)!='qxhhf')   $verif = 0;
            }elseif($srcver==md5('5')){
                if(strtolower($ver)!='vfran')   $verif = 0;
            }elseif($srcver==md5('6')){
                if(strtolower($ver)!='wynv2'and strtolower($ver)!='wyhv2')   $verif = 0;
            }elseif($srcver==md5('7')){
                if(strtolower($ver)!='z5ygq')    $verif = 0;
            }elseif($srcver==md5('8')){
                if(strtolower($ver)!='ygp5k')    $verif = 0;
            }elseif($srcver==md5('9')){
                if(strtolower($ver)!='bb8eb')    $verif = 0;
            }elseif($srcver==md5('10')){
                if(strtolower($ver)!='6xsxm')    $verif = 0;
            }elseif($srcver==md5('11')){
                if(strtolower($ver)!='upsqh')    $verif = 0;
            }elseif($srcver==md5('12')){
                if(strtolower($ver)!='azchy')    $verif = 0;
            }elseif($srcver==md5('13')){
                if(strtolower($ver)!='cyc5p')    $verif = 0;
            }elseif($srcver==md5('14')){
                if(strtolower($ver)!='w6bnp')    $verif = 0;
            }elseif($srcver==md5('15')){
                if(strtolower($ver)!='ukhqu'and strtolower($ver)!='ukhqw')    $verif = 0;
            }elseif($srcver==md5('16')){
                if(strtolower($ver)!='myww7' and strtolower($ver)!='myww2')   $verif = 0;
            }elseif($srcver==md5('17')){
                if(strtolower($ver)!='cumuw')    $verif = 0;
            }elseif($srcver==md5('18')){
                if(strtolower($ver)!='mnmeq')    $verif = 0;
            }elseif($srcver==md5('19')){
                if(strtolower($ver)!='87qba')    $verif = 0;
            }elseif($srcver==md5('20')){
                if(strtolower($ver)!='kmoqe'and strtolower($ver)!='kmqqe')   $verif = 0;
            }
            if($verif == 0){
                echo json_encode(array('stat'=>'400','msg'=>'验证码错误！'),JSON_UNESCAPED_UNICODE);
            }else{
                //注册
                date_default_timezone_set('PRC');
                $logintime=date("Y-m-d H:i");
                $ips = $_SERVER["REMOTE_ADDR"];
				if($ips=='127.0.0.1') $ips='114.114.114.114';
                $wb = file_get_contents('https://apis.map.qq.com/ws/location/v1/ip?key=BEQBZ-ROEKU-MZHVO-BE7UJ-BIXDQ-4XBYS&ip='.$ips); 
                $arr = json_decode($wb, true);
				if($arr){
					$arr = $arr["result"];
					$location = $arr["location"];
					$arr = $arr["ad_info"];
					$nation = $arr["nation"];//国家
					$province = $arr["province"]; //省份
					$city = $arr["city"];//市州
					$district = $arr["district"];//县区
					$adds = $nation.' '.$province.' '.$city.' '.$district;
				}else $adds="中国";
                
                
                
                
                $sql = mysqli_query($conn,"SELECT uid FROM users WHERE username LIKE '$username' ");
                $users = mysqli_fetch_array($sql);
                if($users){
					$uid = $users['uid'];
                    echo json_encode(array('stat'=>'400','msg'=>'用户名已被注册！'),JSON_UNESCAPED_UNICODE);
                }else{
                    //密码MD5加密
                    $password = md5($password);
                    $sql="INSERT INTO users (username,password,qqnum,email,adds,ips,logintime) VALUES('$username','$password','$qqnum','$email','$adds','$ips','$logintime')";
                    $logins = mysqli_query($conn,$sql);
                    //注册验证
                    $sql = mysqli_query($conn,"SELECT uid FROM users WHERE username LIKE '$username' ");
                    $users = mysqli_fetch_array($sql);
                    if($users){
						$uid = $users["uid"];
                        echo json_encode(array('stat'=>'200','msg'=>'注册成功，欢迎加入彪哥.TOP！','username'=>$username,'password'=>$password,'qqnum'=>$qqnum),JSON_UNESCAPED_UNICODE);
                        
						$logonstate = bin2hex(random_bytes(32));
						setcookie("logonstate", $logonstate, time()+3600*24*5,'/');

						$sql="INSERT INTO loginstate (lid,uid,username,qqnum,ints,logintime,state,ipadd) VALUES('$logonstate','$uid','$username','$qqnum',100,'$logintime',2,'$ips')";
						$loginst = mysqli_query($conn,$sql);
                    }else{
                        echo json_encode(array('stat'=>'400','msg'=>'注册失败，请勿输入非法字符！'),JSON_UNESCAPED_UNICODE);
                    }
                }
            }
        }
        
    }elseif(!$fun){
        echo json_encode(array('stat' =>'400','msg' => '请求失败，没有任何参数！'),JSON_UNESCAPED_UNICODE);
    }else{
        echo json_encode(array('stat' =>'400','msg' => '请求失败，参数不合法！'),JSON_UNESCAPED_UNICODE);
    }


?>